package com.bkd.dreams.server.volunteer;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.bkd.dreams.client.login.NotLoggedInException;
import com.bkd.dreams.client.volunteer.Volunteer;
import com.bkd.dreams.client.volunteer.VolunteerDTO;
import com.bkd.dreams.client.volunteer.service.VolunteerService;
import com.bkd.dreams.server.PMF;
import com.bkd.dreams.server.core.RemoteServiceImpl;

public class VolunteerServiceImpl extends RemoteServiceImpl implements VolunteerService {

	@Override
	public void addVolunteer(VolunteerDTO volunteerDTO) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Volunteer volunteer = new Volunteer(volunteerDTO);
			pm.makePersistent(volunteer);
		} finally {
			pm.close();
		}
	}
	
	@Override
	public VolunteerDTO getVolunteerByUserId(String userId) throws Exception {
		checkLoggedIn();
		VolunteerDTO volunteer = VolunteerHelper.getVolunteerByUserId(userId);
		return volunteer;
	}

	@Override
	public List<VolunteerDTO> getAllVolunteers() throws Exception {
		checkLoggedIn();
		List<VolunteerDTO> volunteers = VolunteerHelper.getAllVolunteers();
		return volunteers;
	}

	@Override
	public void toggleAdministrator(VolunteerDTO vol) throws NotLoggedInException {
		checkLoggedIn();
		Volunteer volunteer = VolunteerHelper.getPersistableVolunteerByUserId(vol.getUserId());
		volunteer.setAdministrator(!volunteer.isAdministrator());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(volunteer);
		} finally {
			pm.close();
		}
	}
}
